package com.william.jdbcplus.core.dialect.ddl.index;

import com.william.jdbcplus.common.enums.DbType;
import com.william.jdbcplus.core.dialect.ddl.index.impl.MysqlIndex;
import com.william.jdbcplus.core.dialect.ddl.index.impl.OracleIndex;
import com.william.jdbcplus.core.dialect.ddl.index.impl.SqlServerIndex;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 索引工厂
 *
 * @author william
 */
public class IndexFactory {

    private static final Map<DbType, IIndex> FACTORY_CACHE = new ConcurrentHashMap<>();

    static {
        for (DbType dbType : DbType.values()) {
            FACTORY_CACHE.put(dbType, getByDbType(dbType));
        }
    }

    /**
     * 获取实例
     *
     * @param dbType
     * @return
     */
    public static IIndex getInstance(DbType dbType) {
        IIndex index = FACTORY_CACHE.get(dbType);
        return index;
    }

    /**
     * 根据数据库类型选择不同建表方言
     *
     * @param dbType 数据库类型
     */
    private static IIndex getByDbType(DbType dbType) {
        switch (dbType) {
            case MYSQL:
                return new MysqlIndex();
            case ORACLE:
                return new OracleIndex();
            case SQL_SERVER:
            case SQL_SERVER2005:
                return new SqlServerIndex();
            default:
                return new OracleIndex();
        }
    }
}
